package DivorceSum4;

import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

/**
 * 自定义对象作为数据类型在MR中传递
 */
public class DivorceCountBean implements WritableComparable<DivorceCountBean> {

    private String province;
    private String city;



    public DivorceCountBean(){

    }

    /**
     *对有参构造进行修改，提供一个set方法
     * 自己封装对象的set方法，用于对象属性赋值
     */
    public DivorceCountBean(String province,String city){
        this.province = province;
        this.city = city;
    }

    public void set(String province,String city){
        this.province = province;
        this.city = city;
    }


    //set和get方法


    public String getProvince() {
        return province;
    }

    public void setProvince(String province) {
        this.province = province;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    @Override
    public String toString() {

        return " " +
                "省份=" + province +
                ",市=" + city;
    }

    /**
     * 序列化方法，可以控制把哪写字段序列化出去
     */
    @Override
    public void write(DataOutput dataOutput) throws IOException {
           dataOutput.writeUTF(province);
           dataOutput.writeUTF(city);
    }

    /**
     *  反序列化方法
     *  todo 注意反序列化的顺序和序列化顺序一致
     */
    @Override
    public void readFields(DataInput dataInput) throws IOException {
           this.province = dataInput.readUTF();
           this.city = dataInput.readUTF();
    }


    @Override
    public int compareTo(DivorceCountBean o) {
        int res = this.province.compareTo(o.province);
        if(res!=0){
            return res;
        }else {
            return this.city.compareTo(o.city);
        }
        //return this.province.compareTo(o.getProvince());
    }


}
